package com.bobo.myapplication.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by ZJB on 2018/1/17.
 *
 * @ copyright: iwhere chengdu technology
 */

public class PageView extends View {
    public static final String TAG = "PageView";
    private Paint mPaint,bgPaint,aPaint;
    private Point a, b, c, d, e, f, g, h, i, j, k;
    private Path allPath,pathA;

    public PageView(Context context) {
        this(context, null);
    }

    public PageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        initPoint();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.GRAY);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setTextSize(30);

        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        bgPaint.setColor(Color.BLUE);

        aPaint = new Paint();
        aPaint.setAntiAlias(true);
        aPaint.setColor(Color.WHITE);

        allPath = new Path();
        pathA = new Path();
    }

    private void initPoint() {
        a = new Point();
        b = new Point();
        c = new Point();
        d = new Point();
        e = new Point();
        f = new Point();
        g = new Point();
        h = new Point();
        i = new Point();
        j = new Point();
        k = new Point();
    }


    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                handleCoord(event);
//                handleCoordTrangle(event);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    //计算坐标
    private void handleCoord(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        Log.e(TAG, "handleCoord:x， " + x + "y" + y);

        int width = getWidth();
        int height = getHeight();
        a.set((int) x, (int) y);
        f.set(width, height);

        g.x = (a.x + f.x) / 2;
        g.y = (a.y + f.y) / 2;

        e.x = g.x - (f.y - g.y) * (f.y - g.y) / (f.x - g.x);
        e.y = f.y;

        h.x = f.x;
        h.y = g.y - (f.x - g.x) * (f.x - g.x) / (f.y - g.y);

        c.x = e.x - (f.x - e.x) / 2;
        c.y = f.y;

        j.x = f.x;
        j.y = h.y - (f.y - h.y) / 2;

        b = getIntersectionPoint(a,e,c,j);
        k = getIntersectionPoint(a,h,c,j);

        d.x = (c.x + 2 * e.x + b.x) / 4;
        d.y = (2 * e.y + c.y + b.y) / 4;

        i.x = (j.x + 2 * h.x + k.x) / 4;
        i.y = (2 * h.y + j.y + k.y) / 4;

        postInvalidate();
    }

    /**
     * 计算两线段相交点坐标
     * @return 返回该点
     */
    private Point getIntersectionPoint(Point lineOne_My_pointOne, Point lineOne_My_pointTwo, Point lineTwo_My_pointOne, Point lineTwo_My_pointTwo){
        float x1,y1,x2,y2,x3,y3,x4,y4;
        x1 = lineOne_My_pointOne.x;
        y1 = lineOne_My_pointOne.y;
        x2 = lineOne_My_pointTwo.x;
        y2 = lineOne_My_pointTwo.y;
        x3 = lineTwo_My_pointOne.x;
        y3 = lineTwo_My_pointOne.y;
        x4 = lineTwo_My_pointTwo.x;
        y4 = lineTwo_My_pointTwo.y;

        int pointX = (int) (((x1 - x2) * (x3 * y4 - x4 * y3) - (x3 - x4) * (x1 * y2 - x2 * y1))
                        / ((x3 - x4) * (y1 - y2) - (x1 - x2) * (y3 - y4)));
        int pointY = (int) (((y1 - y2) * (x3 * y4 - x4 * y3) - (x1 * y2 - x2 * y1) * (y3 - y4))
                        / ((y1 - y2) * (x3 - x4) - (x1 - x2) * (y3 - y4)));

        return  new Point(pointX,pointY);
    }


    private void addPath() {
        allPath.reset();
        allPath.lineTo(0, f.y);//移动到左下角
        allPath.lineTo(c.x,c.y);//移动到c点
        allPath.quadTo(e.x,e.y,b.x,b.y);//从c到b画贝塞尔曲线，控制点为e
        allPath.lineTo(a.x,a.y);//移动到a点
        allPath.lineTo(k.x,k.y);//移动到k点
        allPath.quadTo(h.x,h.y,j.x,j.y);//从k到j画贝塞尔曲线，控制点为h
        allPath.lineTo(f.x,0);//移动到右上角
        allPath.close();//闭合区域
    }

    private Path getPathAFromLowerRight(){
        pathA.reset();
        pathA.lineTo(0, f.y);//移动到左下角
        pathA.lineTo(c.x,c.y);//移动到c点
        pathA.quadTo(e.x,e.y,b.x,b.y);//从c到b画贝塞尔曲线，控制点为e
        pathA.lineTo(a.x,a.y);//移动到a点
        pathA.lineTo(k.x,k.y);//移动到k点
        pathA.quadTo(h.x,h.y,j.x,j.y);//从k到j画贝塞尔曲线，控制点为h
        pathA.lineTo(f.x,0);//移动到右上角
        pathA.close();//闭合区域
        return pathA;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e(TAG, "onDraw:j.x， " + j.x + " j.y:" + j.y);
        addBg(canvas);
        addPath();
        addText(canvas);
//        mPaint.setColor(Color.GRAY);
//        mPaint.setStrokeWidth(5);
//        canvas.drawPath(allPath, mPaint);
        drawPathA(canvas);
    }

    private void drawPathA(Canvas canvas) {
        Bitmap bitmap = Bitmap.createBitmap((int) getWidth(), (int) getHeight(), Bitmap.Config.ARGB_8888);
        Canvas bitmapCanvas = new Canvas(bitmap);
        bitmapCanvas.drawPath(getPathAFromLowerRight(),aPaint);
        canvas.drawBitmap(bitmap,0,0,null);

    }

    private void addBg(Canvas canvas) {
        canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),bgPaint);
    }

    private void addText(Canvas canvas) {
        mPaint.setColor(Color.GREEN);
        mPaint.setStrokeWidth(3);
        canvas.drawText("j", j.x, j.y, mPaint);
        canvas.drawText("i", i.x, i.y, mPaint);
        canvas.drawText("h", h.x, h.y, mPaint);
        canvas.drawText("k", k.x, k.y, mPaint);
        canvas.drawText("g", g.x, g.y, mPaint);
        canvas.drawText("a", a.x, a.y, mPaint);
        canvas.drawText("b", b.x, b.y, mPaint);
        canvas.drawText("e", e.x, e.y, mPaint);
        canvas.drawText("f", f.x, f.y, mPaint);
        canvas.drawText("c", c.x, c.y, mPaint);
        canvas.drawText("d", d.x, d.y, mPaint);
    }
}
